{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\thies\OneDrive\00_Promotion\00_Output\00_Paper\2022_Predicting_Econ_Sanctions\Empirics\20231214_Replication\Log_files/01b_Imposition_full_sample_EU.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}18 Dec 2023, 16:07:04
{txt}
{com}. 
. ***************************************************************
. ***EU***
. ***************************************************************
. 
. set seed 1234
{txt}
{com}. 
. *Prepare data
. use "Dataset.dta", clear
{txt}
{com}. keep if sender=="EU"
{txt}(10,154 observations deleted)

{com}. 
. * Independent variables
. gen ln_oil_gas_value_2014 = ln(oil_gas_value_2014+1)
{txt}(681 missing values generated)

{com}. gen sender_colony=0
{txt}
{com}. replace sender_colony=1 if ht_colonial==2 | ht_colonial==3 | ht_colonial==6 | ht_colonial==7 | ht_colonial==8 | ht_colonial==9 | ht_colonial==10
{txt}(3,047 real changes made)

{com}. 
. gen sender_additional=cond(threatUS==1 | impositionUS == 1, 1, 0)
{txt}
{com}. gen only_threat=cond(threatEU==1 & impositionEU == 0, 1, 0)
{txt}
{com}. 
. gen sender_trade = ln_EU_Trade_Eurostat
{txt}(66 missing values generated)

{com}. gen coup_dummy = coup1
{txt}(5 missing values generated)

{com}. replace coup_dummy = 0 if coup_dummy == 1
{txt}(61 real changes made)

{com}. replace coup_dummy = 1 if coup_dummy == 2
{txt}(45 real changes made)

{com}. 
. * Dependent variable : 1 if a threat or sanction case was ongoing in the dyad
. gen sanction_threat = sanction_dyad
{txt}
{com}. replace sanction_threat = 1 if threat_dyad==1
{txt}(49 real changes made)

{com}. tab sanction_threat

{txt}sanction_th {c |}
       reat {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}      4,590       90.41       90.41
{txt}          1 {c |}{res}        487        9.59      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      5,077      100.00
{txt}
{com}. gen sanction_train= sanction_threat if year < 2009
{txt}(1,356 missing values generated)

{com}. gen sanction_test= sanction_threat if year >= 2009
{txt}(3,721 missing values generated)

{com}. 
. * lag time-series variables
. sort ccodecow year
{txt}
{com}. by ccodecow: gen l_v2x_polyarchy = v2x_polyarchy[_n-1] if year==year[_n-1]+1
{txt}(803 missing values generated)

{com}. by ccodecow: gen l_gd_ptss = gd_ptss[_n-1] if year==year[_n-1]+1
{txt}(579 missing values generated)

{com}. by ccodecow: gen l_coup_dummy = coup_dummy[_n-1] if year==year[_n-1]+1
{txt}(203 missing values generated)

{com}. by ccodecow: gen l_one_sided_violence = one_sided_violence[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. by ccodecow: gen l_conflict = conflict[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. by ccodecow: gen l_mid_terr_integrity = mid_terr_integrity[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. by ccodecow: gen l_ln_GDPpc_imputed = ln_GDPpc_imputed[_n-1] if year==year[_n-1]+1
{txt}(318 missing values generated)

{com}. by ccodecow: gen l_sender_trade = sender_trade[_n-1] if year==year[_n-1]+1
{txt}(262 missing values generated)

{com}. by ccodecow: gen l_ln_oil_gas_value = ln_oil_gas_value_2014[_n-1] if year==year[_n-1]+1
{txt}(686 missing values generated)

{com}. by ccodecow: gen l_defense_alliance = defense_alliance[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. 
. * create dummy variables
. tabulate l_gd_ptss, generate (pol_terr)

  {txt}l_gd_ptss {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          1 {c |}{res}      1,130       25.12       25.12
{txt}          2 {c |}{res}      1,263       28.08       53.20
{txt}          3 {c |}{res}      1,212       26.95       80.15
{txt}          4 {c |}{res}        635       14.12       94.26
{txt}          5 {c |}{res}        258        5.74      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      4,498      100.00
{txt}
{com}. 
. * sortieren nach Jahr, zur Vorbereitung RF model
. gen u=0
{txt}
{com}. replace u=1 if year >= 2009
{txt}(1,356 real changes made)

{com}. sort u
{txt}
{com}. 
. ** Imposition
. * Random Forest Model
. rforest sanction_threat l_v2x_polyarchy pol_terr* l_coup_dummy ///
> l_one_sided_violence l_conflict l_mid_terr_integrity ///
> l_ln_GDPpc_imputed l_sender_trade l_ln_oil_gas_value ///
> sender_colony l_defense_alliance in 1/3721, type(class) iter(1500) numvars(15)
{txt}
{com}. 
. *Variable Importance
. ereturn list

{txt}scalars:
       e(Observations) =  {res}3721
           {txt}e(features) =  {res}15
         {txt}e(Iterations) =  {res}1500
          {txt}e(OOB_Error) =  {res}.0636925557645794

{txt}macros:
                e(cmd) : "{res}rforest{txt}"
            e(predict) : "{res}randomforest_predict{txt}"
             e(depvar) : "{res}sanction_threat{txt}"
         e(model_type) : "{res}random forest classification{txt}"

matrices:
         e(importance) : {res} 15 x 1
{txt}
{com}. matrix list e(importance)
{res}
{txt}e(importance)[15,1]
              Variable I~e
l_v2x_poly~y {res}            1
{txt}   pol_terr1 {res}    .33321351
{txt}   pol_terr2 {res}    .54043978
{txt}   pol_terr3 {res}    .93211086
{txt}   pol_terr4 {res}    .99667205
{txt}   pol_terr5 {res}    .60273617
{txt}l_coup_dummy {res}    .45514687
{txt}l_one_side~e {res}    .29709011
{txt}  l_conflict {res}    .60590771
{txt}l_mid_terr~y {res}    .53072051
{txt}l_ln_GDPpc~d {res}    .72652644
{txt}l_sender_t~e {res}    .68175272
{txt}l_ln_oil_g~e {res}    .68991134
{txt}sender_col~y {res}    .20670487
{txt}l_defense_~e {res}    .51603631
{reset}
{com}. * write Variable importance to excel file
. putexcel set "Supplemental_Material\Variable_Importance\Variable_Importance_Imposition_EU_RF_full_sample.xlsx", sheet("M") replace
{res}{txt}Note: File will be replaced when the first {cmd:putexcel} command is issued.

{com}. putexcel A1=matrix(e(importance)), names
{res}{txt}file {bf:Supplemental_Material\Variable_Importance\Variable_Importance_Imposition_EU_RF_full_sample.xlsx} saved

{com}. 
. * Evaluation of model performance
. ** predictions
. predict randonsEU
{txt}
{com}. predict randonsEU0 randonsEU1, pr
{txt}
{com}. 
. * Confusion Matrix
. ** Sensitivity 72.41, Specificity 93.61
. tab2xl sanction_test randonsEU using Supplemental_Material\Prediction_Output\Confusion_Matrixes\EU_Imposition_RF_full_sample, row(1) col(1)
{res}{txt}file {bf:Supplemental_Material\Prediction_Output\Confusion_Matrixes\EU_Imposition_RF_full_sample.xlsx} saved

{com}. diagtest sanction_test randonsEU

{txt}sanction_t {c |}   predicted classes
       est {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
         0 {c |}{res}     1,215         16 {txt}{c |}{res}     1,231 
{txt}         1 {c |}{res}        83         42 {txt}{c |}{res}       125 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}     1,298         58 {txt}{c |}{res}     1,356 

{txt}True D defined as randonsEU ~= 0                      [95% Conf. Inter.]
-------------------------------------------------------------------------
Sensitivity                     Pr( +| D)  {res}72.41%      70.03%   74.79%
{txt}Specificity                     Pr( -|~D)  {res}93.61%      92.30%   94.91%
{txt}Positive predictive value       Pr( D| +)  {res}33.60%      31.09%   36.11%
{txt}Negative predictive value       Pr(~D| -)  {res}98.70%      98.10%   99.30%
{txt}-------------------------------------------------------------------------
Prevalence                      Pr(D)      {res} 4.28%       3.20%    5.35%
{txt}-------------------------------------------------------------------------

{com}. 
. * AUPR .53
. prtab sanction_test randonsEU1, l2title("RF, full sample", box bexpand) 

{txt}{col 12}Number of observations       =  {res}1356
{txt}{col 12}Unique values of classifier  =  {res}1157
{txt}{col 12}Number of positive cases     =  {res}125
{txt}{col 12}Portion of positive cases    ={res}  0.0922

{txt}{hline 50}
{col 5} Recall =  0.1040{col 25}  0.2000{col 37}  0.3040
{hline 50}
{res}{col 2}Precision{col 14}  0.8667{col 25}  0.7353{col 37}  0.7037
{txt}{hline 50}
{res}
{txt}{col 2}Area under precision-recall curve:  0.5280

{com}. graph save "Graph" "Supplemental_Material\Prediction_Output\Roc-curves\AUPR_Imposition_RF_EU_full_sample.gph", replace
{txt}{p 0 4 2}
(file {bf}
Supplemental_Material\Prediction_Output\Roc-curves\AUPR_Imposition_RF_EU_full_sample.gph{rm}
not found)
{p_end}
{res}{txt}file {bf:Supplemental_Material\Prediction_Output\Roc-curves\AUPR_Imposition_RF_EU_full_sample.gph} saved

{com}. 
. * .43
. kap sanction_test randonsEU

{txt}{col 14}Expected
Agreement   agreement     Kappa   Std. err.         Z      Prob>Z
{hline 65}
{res}  92.70%      87.29%     0.4254     0.0250      17.00      0.0000
{txt}
{com}. 
. log close
      {txt}name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\thies\OneDrive\00_Promotion\00_Output\00_Paper\2022_Predicting_Econ_Sanctions\Empirics\20231214_Replication\Log_files/01b_Imposition_full_sample_EU.smcl
  {txt}log type:  {res}smcl
 {txt}closed on:  {res}18 Dec 2023, 16:09:09
{txt}{.-}
{smcl}
{txt}{sf}{ul off}